package indi.kider.server.invoice.service;

import com.google.gson.Gson;
import indi.kider.common.constant.InvoiceDirectionCons;
import indi.kider.common.model.invoice.Invoice;
import indi.kider.common.model.invoice.InvoiceLogQuery;
import indi.kider.common.model.invoice.InvoiceLogs;
import indi.kider.common.util.AuthcUtil;
import indi.kider.server.invoice.mapper.InvoiceLogsMapper;
import indi.kider.server.invoice.mapper.InvoiceMapper;
import indi.kider.server.invoice.related.AuthcService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.vankid.common.model.base.CoreException;
import pers.vankid.common.utils.page.PageRequest;
import pers.vankid.common.utils.page.PageResponse;
import pers.vankid.common.utils.page.PageUtil;

import javax.servlet.http.HttpServletRequest;
import java.beans.Transient;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class InvoiceServiceImpl implements InvoiceService {

    @Autowired
    private InvoiceMapper invoiceMapper;
    @Autowired
    private InvoiceLogsMapper invoiceLogsMapper;
    @Autowired
    private AuthcService authcService;

    /**
     * 买入/拆回 进
     * 报废/装出 出
     *
     * @param logs
     * @return
     */
    @Override
    public List<Invoice> manage(List<InvoiceLogs> logs) throws CoreException {
        if(logs.get(0).getDirection().equals(InvoiceDirectionCons.BUY)||logs.get(0).getDirection().equals(InvoiceDirectionCons.RECOVER))
            return plus(logs);
        else if(logs.get(0).getDirection().equals(InvoiceDirectionCons.INSTALL)||logs.get(0).getDirection().equals(InvoiceDirectionCons.REJECT))
            return minus(logs);
        return null;
    }

    /**
     * 建档
     *
     * @param invoice
     * @return
     */
    @Override
    @Transient
    public Invoice add(Invoice invoice) {
        if (invoice.getNumbs() == null)
            invoice.setNumbs(0);
        if (invoice.getInNumbs() == null)
            invoice.setInNumbs(0);
        if (invoice.getOutNumbs() == null)
            invoice.setOutNumbs(0);
        invoiceMapper.insert(invoice);
        return invoice;
    }

    /**
     * 买入/拆回
     * 进
     *
     * @param logs
     * @return
     */
    @Transient
    public List<Invoice> plus(List<InvoiceLogs> logs) {
        Long employeeId=   authcService.getCurrentUserId().getData();
        List<Invoice> invoices = new ArrayList<>();
        logs.stream().forEach(invoiceLogs -> invoices.add(plus(invoiceLogs,employeeId)));
        return invoices;
    }

    /**
     * 报废/装出
     * 出
     *
     * @param logs
     * @return
     */
    @Transient
    public List<Invoice> minus(List<InvoiceLogs> logs) throws CoreException{
        Long employeeId= authcService.getCurrentUserId().getData();
        List<Invoice> invoices = new ArrayList<>();
            for(InvoiceLogs invoiceLogs:logs){
                invoices.add(minus(invoiceLogs,employeeId));
            }
        return invoices;
    }

    /**
     * 增加库存
     *
     * @param invoiceLogs
     * @return
     */
    private Invoice plus(InvoiceLogs invoiceLogs,Long employeeId) {
        invoiceLogs.setEmployeeId(employeeId);
        invoiceLogs.setInsTime(new Date());
        //更新库存
        Invoice invoice = invoiceMapper.selectByPrimaryKey(invoiceLogs.getInvoiceId());

        invoice.setNumbs(invoice.getNumbs() + invoiceLogs.getNumbs());
        invoice.setInNumbs(invoice.getInNumbs() + invoiceLogs.getNumbs());
        invoiceMapper.updateByPrimaryKeySelective(invoice);
        //插入记录
        invoiceLogsMapper.insert(invoiceLogs);
        return invoice;
    }

    /**
     * 减少库存
     *
     * @param invoiceLogs
     * @return
     */
    private Invoice minus(InvoiceLogs invoiceLogs,Long employeeId) throws CoreException{
        invoiceLogs.setEmployeeId(employeeId);
        invoiceLogs.setInsTime(new Date());
        //更新库存
        Invoice invoice = invoiceMapper.selectByPrimaryKey(invoiceLogs.getInvoiceId());
        if(invoice.getNumbs()>=invoiceLogs.getNumbs()) {
            invoice.setNumbs(invoice.getNumbs() - invoiceLogs.getNumbs());
            invoice.setOutNumbs(invoice.getOutNumbs() + invoiceLogs.getNumbs());
        }else throw new CoreException("库存不足已进行操作，请检查数量",new Gson().toJson(invoice));
        invoiceMapper.updateByPrimaryKeySelective(invoice);
        //插入记录
        invoiceLogsMapper.insert(invoiceLogs);
        return invoice;
    }

    /**
     * 查询资产列表（不分页）
     *
     * @param invoice
     * @return
     */
    @Override
    public List<Invoice> query(Invoice invoice) {
        return invoiceMapper.select(invoice);
    }

    /**
     * 查询资产列表（分页）
     *
     * @param request
     * @return
     */
    @Override
    public PageResponse<Invoice> query(PageRequest<Invoice> request) {
        PageUtil.start(request);
        return PageUtil.getPageResponse(invoiceMapper.queryLike(request.getQuery()));
    }

    /**
     * 查询资产操作列表（分页）
     *
     * @param request
     * @return
     */
    @Override
    public PageResponse<InvoiceLogQuery> queryLogs(PageRequest<InvoiceLogQuery> request) {
        PageUtil.start(request);
        return PageUtil.getPageResponse(invoiceLogsMapper.queryLogs(request.getQuery()));
    }
}
